package com.设计模式.observer;


import java.util.ArrayList;

/** 具体的实现类 注册中心
 * @Date 2022/9/7 13:28
 * @Author WangLinBao
 * @Description
 */
public class WeatherData extends Client implements Subject{
    private String temperature;
    private String humidity;
    private ArrayList<Observer> observerArrayList;
    public WeatherData(String temperature,String humidity){
        this.temperature = temperature;
        this.humidity = humidity;
        observerArrayList = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer observer) {
        observerArrayList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if(observerArrayList.contains(observer)){
            observerArrayList.remove(observer);
        }
    }

    @Override
    public void notifyObservers() {
        //通知所有注册的观察者
        for (Observer observer : observerArrayList) {
            observer.update(temperature,humidity);
        }
    }

    public String getTemperature() {
        return temperature;
    }

    public void setData(String temperature,String humidity) {
        this.temperature = temperature;
        this.humidity = humidity;
        notifyObservers();
    }

    public String getHumidity() {
        return humidity;
    }


}
